<?php

namespace app\admin\model;

use app\common\model\Tags;
use app\common\model\UserCollect;
use think\facade\Db;
use think\Model;

/**
 * Houses
 */
class Houses extends Model
{
    // 表名
    protected $name = 'houses';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;


    // 追加属性
    protected $append = [
        'city_text',
        'status_text',
        'category_text',
        'address_text',
        'surround_text',
        'property_text',
        'industry_text',
        'seal_status_text',
        'paymethod_text',
        'domain_images',
        'other_tags_text',
        'rent_unit_text',
        'check_text'
    ];

    public function getCheckTextAttr($value, $data)
    {
        if (isset($data['check'])) {
            return json_decode($data['check'], true);
        }
        return [];
    }

    //某某人找某地区某大小铺面做某行业
    public function setNameAttr($value, $data)
    {
        $contact_person_str = isset($data['contact_person']) ? $data['contact_person'] : '';
        if (isset($data['user_id'])) {
            $contact_person_str = (new User())->where(['id' => $data['user_id']])->value('nickname', '');
        }
        $acreage = isset($data['acreage']) ? $data['acreage'] . '㎡' : '';
        $str2 = '';
        $city = isset($data['city']) ? $data['city'] : '';
        $city_name = '';
        if (!empty($city)) {
            $city_name = Db::name('area')
                ->whereIn('id', $city)
                ->where(['level' => 3])
                ->cache(true)
                ->value('name');
        }
        $str2 = $city_name . $acreage;
        $industry_ids = isset($data['industry']) ? $data['industry'] : '';
        $industry_names = Db::name('tags')->whereIn('id', $industry_ids)->order('weigh', 'desc')->limit(3)
            ->column('string');
        $str2 .= implode(',', $industry_names);
        return $str2 . '转租';
//        $area=
//        $qu_name=Db::name('area')->where(['id'=>])->cache(true)->value('name');
    }


    public function getRentUnitList()
    {
        //租金单位:0=元/月,1=元/㎡/月
        return ['0' => __('元/月'), '1' => __('元/㎡/月'), 2 => "元/年"];
    }

    public function getRentUnitTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['rent_unit']) ? $data['rent_unit'] : '');
        $list = $this->getRentUnitList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getDomainImagesAttr($value, $data)
    {
        $domain = request()->domain();
        $images = $data['images'];
        if (empty($images)) return '';
        $images = explode(',', $images);
        $domain_images = [];
        foreach ($images as $image) {
            $domain_images[] = $domain . $image;
        }
        return $domain_images;
    }

    public function getPaymethodTextAttr($value, $data)
    {
        $seal_status = [1 => "押一付一", "3" => "押一付三", "10" => "押一付十", "6" => "半年付", "12" => "年付"];
        return isset($seal_status[$data['paymethod']]) ? $seal_status[$data['paymethod']] : '_';
    }

    public function getStatusTextAttr($value, $data)
    {
        $seal_status = [0 => '已下架', 1 => '已上架', 2 => '待审核', 3 => '已驳回', 4 => '佣金已支付'];
        return isset($seal_status[$data['status']]) ? $seal_status[$data['status']] : '-';
    }

    public function getSealStatusTextAttr($value, $data)
    {
        $seal_status = [0 => '未出售', 1 => '已售出'];
        return $seal_status[$data['seal_status']];
    }

    public function getCategoryTextAttr($value, $data)
    {
        return (new Category())->whereIn('id', $data['category_ids'])->column('name');
    }

    public function getAddressTextAttr($value, $data)
    {
        if ($data['city'] === '' || $data['city'] === null) return '';
        $cityNames = \think\facade\Db::name('area')->whereIn('id', $data['city'])->where(['level' => 3])->value('name');
        $address = $data['address'];
        return $cityNames ? $cityNames . '/' . $address : $address;
    }

    protected static function onAfterInsert($model)
    {
        if ($model->weigh == 0) {
            $pk = $model->getPk();
            if (strlen($model[$pk]) >= 19) {
                $model->where($pk, $model[$pk])->update(['weigh' => $model->count()]);
            } else {
                $model->where($pk, $model[$pk])->update(['weigh' => $model[$pk]]);
            }
        }
    }


    protected static function onAfterDelete($model)
    {
        $pk = $model->getPk();
        (new UserCollect())
            ->where([
                'type' => 'house',
                'houses_id' => $model->column($pk)
            ])->delete();
    }

    public function getCategoryIdsAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setCategoryIdsAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getCityAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setCityAttr($value): string
    {
        $this->set('seach_area', is_array($value) ? $value[count($value) - 1] : $value);
        return is_array($value) ? implode(',', $value) : $value;
    }


//    public function setSeachAreaAttr($value, $data)
//    {
//        $last_city = isset($data['city']) ? $data['city'] : '';
//        if ($last_city) {
//            if (is_array($last_city)) {
//                return $last_city[count($last_city) - 1];
//            } else {
//                $lsts_city = explode(',', $last_city);
//                return $lsts_city[count($last_city) - 1];
//            }
//        }
//        return '12';
//    }
    public function getCityTextAttr($value, $row): string
    {
        if ($row['city'] === '' || $row['city'] === null) return '';
        $cityNames = \think\facade\Db::name('area')->whereIn('id', $row['city'])->column('name');
        return $cityNames ? implode(',', $cityNames) : '';
    }

    public function getImagesAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setImagesAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getFilesAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setFilesAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }


    public function getPropertyAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setPropertyAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function setIndustryAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getIndustryAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function getSurroundAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setSurroundAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }


    public function getSurroundTextAttr($value, $row): array
    {
        return (new Tags())->whereIn('id', $row['surround'])->column('string');
    }

    public function getIndustryTextAttr($value, $row): array
    {
        if (empty($row['industry'])) {
            return ['不限行业'];
        }
        return (new Tags())->whereIn('id', $row['industry'])->column('string');
    }

    public function getPropertyTextAttr($value, $row): array
    {
        return (new Tags())->whereIn('id', $row['property'])->field('string,image')->select()->toArray();
    }

    public function user(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\common\model\User::class, 'user_id', 'id');
    }

    public function getOtherTagsAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function getOtherTagsTextAttr($value, $row): array
    {
        return (new Tags())->whereIn('id', $row['other_tags'])->column('string');
    }

    public function setOtherTagsAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }


}
